<?php


namespace app\admin\controller;

use app\logic\DataHelper;
use app\logic\ArticleHelper;
use app\logic\MenuHelper;
use think\facade\Db;
use think\facade\View;

class ArticleCategory extends Base
{
    public function index() {
        return View::fetch('index');
    }

    public function load_category() {
        $tree = ArticleHelper::load_article_category(0); // MenuHelper::load_menu(0);

        $tree = array(
            [
                'id' => 0,
                'pid' => 0,
                'pname' => '',
                'title' => '根节点',
                'status' => 0,
                'spread' => true,
                'children' => $tree
            ]
        );

        return json([
            'code' => 0,
            'msg' => '成功',
            'tree' => $tree
        ]);
    }

    public function del() {
        if (!check_params_empty(['id'])) {
            return json([ 'code' => -1, 'msg' => '请填写：id' ]);
        }

        $params = get_all_params();

        DataHelper::update('tb_article_category', "id=".$params['id'], [
            'status' => -1
        ]);

        return json([ 'code' => 0, 'msg' => '成功' ]);
    }

    public function save() {
        $params = get_all_params();

//        if (!$params['id'] && !$params['pid']) {
//            return json([ 'code' => -1, 'msg' => '不能编辑根节点' ]);
//        }

        if (!check_params_empty(['title', 'idx'])) {
            return json([ 'code' => -1, 'msg' => '请填写：分类名称,排序值' ]);
        }

        if (isset($params['id']) && $params['id'] > 0) {
            //更新
            DataHelper::update('tb_article_category', "id=".$params['id'], $params);
        } else {
            //新增
            DataHelper::add('tb_article_category', $params);
        }

        return json([
            'code' => 0,
            'msg' => '成功'
        ]);
    }
}